/**
 * @(#)RefererController.java 2017年4月10日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.annotation.SystemControllerLog;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.channel.Referer;
import org.jsbd.boss.service.IRefererService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * TODO 请加入本类的说明
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@RequestMapping("/referer")
@Controller
public class RefererController {
	public static final Logger LOGGER = LoggerFactory.getLogger(RefererController.class);

	@Resource
	private IRefererService refererService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list() {
		return "/referer/list";
	}

	@RequestMapping(value = "listdata")
	@ResponseBody
	public Map<String, Object> listdata(Referer referer) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Integer isUseful = referer.getIsUseful();
			if (isUseful != null && isUseful == -1) {
				referer.setIsUseful(null);
			}
			map.put("total", refererService.selectCountOfReferer(referer));
			map.put("rows", refererService.selectPageRefererList(referer));
			return map;
		} catch (Exception e) {
			LOGGER.error("查询出错", e);
		}
		return null;
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "/referer/add";
	}

	@RequestMapping(value = "effect")
	@SystemControllerLog(description = "生效")
	public void effect(Integer id, HttpServletResponse response) {
		try {
			Referer referer = refererService.selectReferer(id);
			referer.setIsUseful(1);
			refererService.updateReferer(referer);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("生效referer出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "abandon")
	@SystemControllerLog(description = "作废")
	public void abandon(Integer id, HttpServletResponse response) {
		try {
			Referer referer = refererService.selectReferer(id);
			referer.setIsUseful(0);
			refererService.updateReferer(referer);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("作废referer出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@SystemControllerLog(description = "新增配置")
	public void save(Referer referer, HttpServletResponse response) {
		try {
			refererService.insertReferer(referer);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("保存referer出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@SystemControllerLog(description = "更新配置")
	public void update(Referer referer, HttpServletResponse response) {
		try {
			refererService.updateReferer(referer);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("更新referer出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "{id}/update")
	public String update(@PathVariable Integer id, Model model) {
		Referer referer = refererService.selectReferer(id);
		model.addAttribute("referer", referer);
		return "/referer/update";
	}

}
